<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="title"
    top="5vh"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <div class="park-fee-form-body">
      <el-form ref="form" label-width="110px" :model="form" :rules="rules">
        <el-form-item label="停车时长" prop="timeUnitNum">
          <el-input-number v-model.trim="form.timeUnitNum" />
        </el-form-item>
        <el-form-item label="时间单位" prop="timeUnit">
          <el-select
            v-model="form.timeUnit"
            class="full-select"
            clearable
            placeholder="请选择时间单位"
          >
            <el-option
              v-for="item in timeUnitDict"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="价位" prop="price">
          <el-input-number v-model.trim="form.price" />
        </el-form-item>
        <el-form-item label="小区名称" prop="areaNo">
          <el-select
            v-model="form.areaNo"
            class="full-select"
            clearable
            placeholder="请选择小区名称"
          >
            <el-option
              v-for="item in cellCode"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { saveOrUpdate } from '@/api/park'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ParkFeeEdit',
    data() {
      return {
        form: {
          timeUnit: '',
          timeUnitNum: '',
          price: '',
          areaNo: '',
        },
        timeUnitDict: [
          { label: '分钟', value: 0 },
          { label: '小时', value: 1 },
        ],
        rules: {
          timeUnit: [
            { required: true, trigger: 'change', message: '请输入时间单位' },
          ],
          timeUnitNum: [
            { required: true, trigger: 'blur', message: '请输入停车时长' },
          ],
          price: [{ required: true, trigger: 'blur', message: '请输入价格' }],
          areaNo: [
            { required: true, trigger: 'change', message: '请选择小区名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        townList: [],
        areaList: [],
        isSaving: false,
      }
    },
    computed: {
      ...mapGetters({
        cellCode: 'dict/cellCode',
      }),
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加停车计费标准'
        } else {
          this.title = '修改停车计费标准'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              this.isSaving = true
              const res = await saveOrUpdate(this.form)
              let successMsg = '添加成功'
              let errorMsg = '添加失败'
              if (this.form.id) {
                successMsg = '修改成功'
                errorMsg = '修改成功'
              }
              if (res && res.status === 0) {
                this.$baseMessage(
                  res.msg || successMsg,
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(
                  res ? res.msg : errorMsg,
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e.message || '操作失败',
                'error',
                'vab-hey-message-error'
              )
            }
            this.isSaving = false
          }
        })
      },
    },
  }
</script>
<style lang="css">
  .full-select {
    width: 100%;
  }
  .park-fee-form-body {
    padding: 0;
    height: 200px;
  }
</style>
